package com.iss.card.view.mycard.detail;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.iss.card.R;
import com.iss.card.cache.ImageDataManager;
import com.iss.card.common.ApplicationManager;
import com.iss.card.common.CardTypeUtil;
import com.iss.card.common.FunctionUtil;
import com.iss.card.common.IBackKeyListener;
import com.iss.card.model.UserInfo;
import com.iss.card.view.model.MycardDetailModel;
import com.iss.card.view.model.ResponseBean;
import com.iss.card.view.mycard.CardBagModel;
import com.iss.card.view.overrideview.CardDetailMyCardInfoView;
import com.iss.card.webservice.BaseModel;
import com.iss.card.webservice.WebServiceAsyncService;
import com.iss.card.webservice.WebServiceAsyncService.Operation;
import com.iss.card.webservice.WebServiceAsyncService.PostExecute;
import com.iss.card.webservice.WebServiceAsyncServiceHelper;
import com.iss.card.webservice.WebServiceManager.WebServiceRegistrationParameter;
import com.iss.card.webservice.WebServiceManager.WebServiceUrlPortManager;
import com.iss.card.webservice.WebServiceUtils;

public class CardDetailMyCardFragment extends Fragment implements
		IBackKeyListener {

	private View mainView;
	private FragmentActivity mContext;
	private static final String TAG = "CardDetailMyCardFragment";
	private MycardDetailModel myCardModel;
	private UserInfo usrInfo;
	private CardBagModel mBriefCardInfo;

	private void initView() {

		TextView tvCardIssuer, tvCardOwner, tvLegalClaim;
		ImageView imgCard;
		tvCardIssuer = (TextView) mainView
				.findViewById(R.id.tv_card_detail_mycard_store_name);
		tvCardOwner = (TextView) mainView
				.findViewById(R.id.tv_card_detail_mycard_user_name);

		tvLegalClaim = (TextView) mainView
				.findViewById(R.id.tv_card_detail_mycard_copyright);
		imgCard = (ImageView) mainView
				.findViewById(R.id.iv_card_detail_mycard_image);
		CardDetailMyCardInfoView remainInfo = (CardDetailMyCardInfoView) mainView
				.findViewById(R.id.tv_card_detail_mycard_remain_info);
		if (myCardModel != null) {
			tvCardIssuer.setText(myCardModel.getcardIssuer());
			tvCardOwner.setText(String.format(
					getString(R.string.card_detail_mycard_ownername),
					myCardModel.getcardOwner()));
			remainInfo.setCardTypeInformation(
					CardTypeUtil.getCardTypeByAttributes(
							mBriefCardInfo.getCardType(),
							mBriefCardInfo.getCardAttr()),
					myCardModel.geTremainNumber(),
					myCardModel.getRemainPoints(),
					myCardModel.getRemainValue(), myCardModel.getDisplayedDiscount());
			ImageDataManager.instance().loadBitmap(myCardModel.getCardPicUrl(),
					imgCard, null);
			tvLegalClaim.setText(String.format(
					getString(R.string.card_detail_legal_declaration),
					myCardModel.getcardIssuer()));
		}
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		this.mContext = (FragmentActivity) activity;

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		usrInfo = FunctionUtil.getCurrentLogInUser();
		mBriefCardInfo = (CardBagModel) getArguments().getSerializable(
				ApplicationManager.KEY_MYCARD_BRIEF_CARD);
		/*
		 * mBriefCardInfo.setCardAttr("1,2,3"); mBriefCardInfo.setCardType("2");
		 */
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		// mainView = new View(mContext).inflate(mContext, R.layout.card_mycard,
		// null);
		mainView = inflater.inflate(R.layout.card_detail_mycard, null);
		getCardDetailMycardInfo(mBriefCardInfo.getCardId(),
				usrInfo.getUsrMobile());
		return mainView;
	}

	private void getCardDetailMycardInfo(String id, String mobile) {

		String obj[] = {id, mobile };
		WebServiceAsyncService asyncService = new WebServiceAsyncService();
		asyncService.setParameterToAsyncService(mContext,
				asyncService.getNextToken(),
				Operation.OP_SERVICE_ARG_VIP_ORDER,
				WebServiceRegistrationParameter.PRODUCT_VIP_ORDER_PARAMETER,
				obj, WebServiceUrlPortManager.ACTION_URL_PORT_VIP_ORDER, this,
				WebServiceAsyncServiceHelper.TYPE_HTTP_GET_INSTREAM);
		asyncService.setPostExecute(new PostExecute() {
			@Override
			public void onAsyncServiceComplete(int token, Object envelope,
					int index) {
				if (envelope == null) {
					Log.e(TAG, "envelope is null");
					return;
				}

				ResponseBean response = WebServiceUtils
						.getObjectResultToBaseInfo(envelope,
								MycardDetailModel.class);

				List<? extends BaseModel> list = response.getReturnList();
				myCardModel = null;				
				for (BaseModel info : list) {
					myCardModel = (MycardDetailModel) info;
					initView();
					break;
				}
			}
		});
	}

	@Override
	public void cancelUpdate(Object... params) {
		// TODO Auto-generated method stub

	}
}
